package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.GoodsInfo;
import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.mapper.GoodsInfoMapper;
import com.ktjiaoyu.service.GoodsInfoService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("GoodsInfoService")
public class GoodsInfoServiceImpl implements GoodsInfoService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public List<GoodsInfo> findList(GoodsInfo goodsInfo) {
        return null;
    }

    @Override
    public Page<GoodsInfo> findPage(GoodsInfo entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<GoodsInfo> page=null;
        try{
            page=new Page<GoodsInfo>();
            page.setPageSize(pageSize);
            Long count= goodsInfoMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);

            List<GoodsInfo> data=goodsInfoMapper.getPage(entity,page.offset(),page.getPageSize());

            page.setData(data);
        }catch (Exception e){
            LOGGER.error("GoodsInfoServiceImpl.findPage(GoodsInfo,Integer,Integer)方法出现异常。",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(GoodsInfo entity) throws Exception {
        try {
            if (entity.getId()==null){
                return goodsInfoMapper.insert(entity)>0;
            }else {
                return goodsInfoMapper.update(entity)>0;
            }
        }catch (Exception e){
            LOGGER.error("GoodsInfoServiceImpl.saveOrUpdate(GoodsInfo)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public GoodsInfo findById(Long id) throws Exception {
        try{
            return goodsInfoMapper.getById(id);
        }catch (Exception e){
            LOGGER.error("GoodsInfoServiceImpl.findById(Long)方法出现异常。",e);
            throw e;
        }
    }


    @Override
    public int deleteById(Long id) throws Exception {
        try{
            return goodsInfoMapper.deleteById(id);
        }catch (Exception e){
            LOGGER.error("GoodsInfoServiceImpl.deleteById(Long)方法出现异常。",e);
            throw e;
        }
    }
}
